package it.itsvil.servlet;

import it.itsvil.bean.user.UserBean;
import it.itsvil.ejb.session.RequestRemote;
import it.itsvil.ejb.session.RequestRemoteHome;

import java.io.IOException;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	ServletContext servletContext = null;

	/**
	 * Default constructor.
	 */
	public LoginServlet() {
		// TODO Auto-generated constructor stub
	}

	private Properties getInitialContextProps() {
		Properties props = new Properties();
		props.put(Context.INITIAL_CONTEXT_FACTORY,
				"org.jnp.interfaces.NamingContextFactory");
		props.put(Context.URL_PKG_PREFIXES,
				"org.jboss.naming:org.jnp.interfaces");
		props.put(Context.PROVIDER_URL, "jnp://localhost:1099");
		props.put(Context.SECURITY_PRINCIPAL, "admin");
		props.put(Context.SECURITY_CREDENTIALS, "admin");
		return props;
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		HttpSession session = request.getSession(true);

		servletContext = session.getServletContext();

		try {
			System.out.println("In the Login Servlet");
			UserBean userBean = new UserBean();
			userBean.setEmail(request.getParameter("email"));
			userBean.setPassword(request.getParameter("pass"));
			Context initial = new InitialContext(getInitialContextProps());

			Object objref = initial.lookup("MyRequestBean");
			System.out.println("Lookup done");
			RequestRemoteHome home = (RequestRemoteHome) PortableRemoteObject
					.narrow(objref, RequestRemoteHome.class);

			System.out.println("Lookup home done");
			RequestRemote requestRemote = home.create();
			System.out.println("Request lost");
			UserBean current = requestRemote.isLogged(userBean);
			System.out.println("isLogged() called");
			if (current != null) {
				session.setAttribute("currentSessionUser", current);
				session.setAttribute("username", current.getSurname() + " "
						+ current.getName());
				response.sendRedirect("main.jsp");
			} else {
				request.setAttribute("standard_error", "Repeat password");
				RequestDispatcher rd = request
						.getRequestDispatcher("index.jsp");
				rd.forward(request, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
